package com.wzy.sharepreferenceutil;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Created by wangzhenyu on 2016/7/23.
 */
public class BaseSharePreference {
    private SharedPreferences mSharePreferences;

    public BaseSharePreference() {

    }

    public void init(Context context, String sharedFileName) {
        if (TextUtils.isEmpty(sharedFileName)) {
            throw new RuntimeException("sharedFileName can\'t be null");
        } else {
            this.mSharePreferences = context.getSharedPreferences(sharedFileName, 0);
        }
    }

    public SharedPreferences getSharedPreferences() {
        return this.mSharePreferences;
    }

    public void putString(String key, String value) {
        SharedPreferences.Editor editor = this.mSharePreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void putInt(String key, int value) {
        SharedPreferences.Editor editor = this.mSharePreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public void putLong(String key, long value) {
        SharedPreferences.Editor editor = this.mSharePreferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public void putFloat(String key, float value) {
        SharedPreferences.Editor editor = this.mSharePreferences.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = this.mSharePreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public String getString(String key, String defValue) {
        if (this.mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        } else {
            return this.mSharePreferences.getString(key, defValue);
        }
    }

    public boolean getBoolean(String key, boolean defValue) {
        if (this.mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        } else {
            return this.mSharePreferences.getBoolean(key, defValue);
        }
    }

    public int getInt(String key, int defValue) {
        if (this.mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        } else {
            return this.mSharePreferences.getInt(key, defValue);
        }
    }

    public long getLong(String key, long defValue) {
        if (this.mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        } else {
            return this.mSharePreferences.getLong(key, defValue);
        }
    }

    public float getFloat(String key, float defValue) {
        if (this.mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        } else {
            return this.mSharePreferences.getFloat(key, defValue);
        }
    }

    public boolean isKeyExist(String key) {
        return this.mSharePreferences == null ? false : this.mSharePreferences.contains(key);
    }

    public void remove(String key) {
        if (this.mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        } else {
            SharedPreferences.Editor editor = this.mSharePreferences.edit();
            if (this.mSharePreferences.contains(key)) {
                editor.remove(key);
                editor.commit();
            }

        }
    }

    public void release() {
        this.mSharePreferences = null;
    }

}
